package commEntity

import (
	"encoding/json"
)

type DeviceObjectEntity struct {
	BaseEntity *BaseEntity // 基本信息

	Manufacturer string // 设备厂商名称
	DeviceType   string // 设备类型名
	DeviceName   string // 设备名称

	ObjectName string // 通道名称
}

type deviceObjectPo struct {
	Id           int64  `json:"id" db:"id"`                     // 对象id
	CreateTime   int64  `json:"createTime" db:"create_time"`    // 创建时间
	UpdateTime   int64  `json:"updateTime" db:"update_time"`    // 更新时间
	Manufacturer string `json:"manufacturer" db:"manufacturer"` // 设备厂商
	DeviceType   string `json:"deviceType" db:"device_type"`    // 设备类型
	DeviceName   string `json:"deviceName" db:"device_name"`    // 设备名称
	ObjectName   string `json:"objectName" db:"object_name"`    // 对象名称
}

func (e *DeviceObjectEntity) Type() string {
	return "DeviceObjectEntity"
}

func (e *DeviceObjectEntity) TableName() string {
	return "tb_device_object"
}

func (e *DeviceObjectEntity) GetBaseEntity() *BaseEntity {
	return e.BaseEntity
}

func (e *DeviceObjectEntity) Instance() *DeviceObjectEntity {
	return &DeviceObjectEntity{
		BaseEntity: new(BaseEntity),
	}
}

func (e *DeviceObjectEntity) NewEntity() IEntity {
	return e.Instance()
}

func (e *DeviceObjectEntity) NewPo() interface{} {
	return &deviceObjectPo{}
}

func (e *DeviceObjectEntity) MakeServiceKey() string {
	p := e.MakeServiceKeyList()
	return MakeServiceKey(p)
}

func (e *DeviceObjectEntity) MakeServiceKeyList() []string {
	var p []string
	p = append(p, e.DeviceName)
	p = append(p, e.ObjectName)
	return p
}

func (e *DeviceObjectEntity) BuildByJson(jsn *string) error {
	var po deviceObjectPo
	err := json.Unmarshal([]byte(*jsn), &po)
	if err != nil {
		return err
	}

	e.BuildByPo(&po)
	return nil
}

func (e *DeviceObjectEntity) BuildToJson() (string, error) {
	po := e.BuildToPo()
	code, err := json.Marshal(po)
	if err != nil {
		return "", err
	}

	return string(code), nil
}

func (e *DeviceObjectEntity) BuildByPo(pop interface{}) {
	ptr := e
	po := pop.(*deviceObjectPo)

	ptr.BaseEntity.Id = po.Id
	ptr.BaseEntity.CreateTime = po.CreateTime
	ptr.BaseEntity.UpdateTime = po.UpdateTime
	ptr.Manufacturer = po.Manufacturer
	ptr.DeviceType = po.DeviceType
	ptr.DeviceName = po.DeviceName
	ptr.ObjectName = po.ObjectName
}

func (e *DeviceObjectEntity) BuildToPo() interface{} {
	ptr := e
	po := deviceObjectPo{}

	po.Id = ptr.BaseEntity.Id
	po.CreateTime = ptr.BaseEntity.CreateTime
	po.UpdateTime = ptr.BaseEntity.UpdateTime
	po.Manufacturer = ptr.Manufacturer
	po.DeviceType = ptr.DeviceType
	po.DeviceName = ptr.DeviceName
	po.ObjectName = ptr.ObjectName

	return &po
}
